<template>
  <div class="activity-favorites">
    <van-nav-bar
      title="收藏的活动"
      left-arrow
      @click-left="onClickLeft"
    />
    
    <div class="content">
      <template v-if="!loading">
        <div class="activity-list" v-if="activities.length > 0">
          <activity-card 
            v-for="activity in activities" 
            :key="activity.id" 
            :activity="activity"
            @click="goToDetail(activity.id)"
          />
        </div>
        <van-empty v-else description="暂无收藏的活动" />
      </template>
      
      <div class="loading-container" v-if="loading">
        <van-loading type="spinner" color="#8a2be2" size="36px">加载中...</van-loading>
      </div>
      
      <!-- 加载更多 -->
      <div class="load-more" v-if="hasMore && !loading && activities.length > 0" @click="loadMore">
        <span>加载更多</span>
      </div>
    </div>
  </div>
</template>

<script setup lang="ts">
import { ref, onMounted } from 'vue';
import { useRouter } from 'vue-router';
import { showToast } from 'vant';
import ActivityCard from '@/components/activity/ActivityCard.vue';
import { useActivityStore } from '@/stores/activity';

const router = useRouter();
const activityStore = useActivityStore();
const activities = ref([]);
const loading = ref(true);
const hasMore = ref(true);
const currentPage = ref(1);
const pageSize = ref(10);

// 获取收藏的活动列表
const fetchFavoriteActivities = async (page = 1, size = 10) => {
  try {
    loading.value = true;
    
    // 这里应该调用获取收藏活动的API
    // const result = await activityStore.getFavoriteActivities(page, size);
    
    // 模拟API调用
    await new Promise(resolve => setTimeout(resolve, 1000));
    
    // 模拟数据
    const mockData = Array(size).fill(0).map((_, index) => {
      const id = (page - 1) * size + index + 1;
      return {
        id: `${id}`,
        title: `收藏的活动 ${id}`,
        description: `这是收藏的活动描述 ${id}`,
        location: '杭州市西湖区',
        startTime: new Date().toISOString(),
        cover: `https://picsum.photos/300/400?random=${id}`,
        tags: ['休闲骑行', '环湖骑行']
      };
    });
    
    if (page === 1) {
      activities.value = mockData;
    } else {
      activities.value = [...activities.value, ...mockData];
    }
    
    // 模拟没有更多数据
    if (page >= 3) {
      hasMore.value = false;
    }
    
  } catch (error) {
    console.error('获取收藏活动失败:', error);
    showToast('获取收藏活动失败');
  } finally {
    loading.value = false;
  }
};

// 加载更多
const loadMore = () => {
  if (loading.value || !hasMore.value) return;
  
  currentPage.value += 1;
  fetchFavoriteActivities(currentPage.value, pageSize.value);
};

// 跳转到活动详情
const goToDetail = (id) => {
  router.push(`/activity/detail/${id}`);
};

// 返回上一页
const onClickLeft = () => {
  router.back();
};

// 页面加载时获取收藏活动列表
onMounted(() => {
  fetchFavoriteActivities(currentPage.value, pageSize.value);
});
</script>

<style scoped lang="scss">
.activity-favorites {
  min-height: 100vh;
  background-color: #f7f8fa;
}

.content {
  padding: 16px;
}

.activity-list {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.loading-container {
  display: flex;
  justify-content: center;
  align-items: center;
  height: 200px;
}

.load-more {
  text-align: center;
  color: #8a2be2;
  padding: 16px 0;
  font-size: 14px;
}
</style>